package com.wei.tang.adapter;

import java.util.List;

import com.wei.tang.R;
import com.wei.tang.bean.CourseBean;
import com.wei.tang.util.Util;

import android.annotation.SuppressLint;
import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

public class HorizontalCourseAdapter extends BaseAdapter {
	private Context mContext;
	private LayoutInflater mInflater;
	private List<CourseBean> datas;

	public HorizontalCourseAdapter(Context _context, List<CourseBean> _datas) {
		this.mContext = _context;
		this.datas = _datas;
		mInflater = (LayoutInflater) mContext.getSystemService(Context.LAYOUT_INFLATER_SERVICE);// LayoutInflater.from(mContext);
	}

	@Override
	public int getCount() {
		return datas.size();
	}

	@Override
	public Object getItem(int position) {
		return position;
	}

	@Override
	public long getItemId(int position) {
		return position;
	}

	@SuppressLint("NewApi")
	@Override
	public View getView(final int position, View convertView, ViewGroup parent) {
		ViewHolder holder;
		if (convertView == null) {
			holder = new ViewHolder();
			convertView = mInflater.inflate(R.layout.course_information_fragment_item, null);
			holder.iv_couser_info_iamge = (ImageView) convertView.findViewById(R.id.iv_couser_info_iamge);
			holder.tv_couser_info_name = (TextView) convertView.findViewById(R.id.tv_couser_info_name);
			holder.tv_couser_info_time = (TextView) convertView.findViewById(R.id.tv_couser_info_time);
			holder.tv_couser_info_number = (TextView) convertView.findViewById(R.id.tv_couser_info_number);
			convertView.setTag(holder);
		} else {
			holder = (ViewHolder) convertView.getTag();
		}

		/**
		 * url不仅支持网络图片显示，同时支持本地SD卡上的图片显示；
		 * view不仅可以是imageview，同时普通view也可以传入，框架会自动识别对imageview设置src对普通view设置bg
		 */
		holder.iv_couser_info_iamge
				.setImageResource(Util.getResId(datas.get(position).getCourse_size(), R.drawable.class));
		holder.tv_couser_info_name.setText(datas.get(position).getCourse_name());
		holder.tv_couser_info_time.setText(datas.get(position).getCourse_time());
		holder.tv_couser_info_number.setText(datas.get(position).getCourse_playtimes());

		return convertView;
	}

	private static class ViewHolder {
		public ImageView iv_couser_info_iamge;
		public TextView tv_couser_info_name, tv_couser_info_time, tv_couser_info_number;
	}
}